import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Login.vue'


import StuRouter from "@/views/student/router";
import TeacherRouter from "@/views/teacher/router"

import { MessageBox } from "element-ui"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Home
  },

  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue')

  },
  {
    path: "/main",
    name: 'main',
    component: () => import('@/views/Main.vue'),
    children: [
      ...StuRouter,
      ...TeacherRouter,
    ]
  },
  {
    path: "*",
    redirect: "/login"
  }

]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})


function showAlert() {
  MessageBox.alert("你暂无访问权限...", "警告", {
    confirmButtonText: '确定',
    type: "warning",
    callback: action => {
      router.push({ name: "login" })
    }
  })
}

router.beforeEach((to, from, next) => {
  console.log(to);

  if (localStorage.rolltype == "2") {
    // 教师身份 
    if (to.name.indexOf("stu") > -1) {
      showAlert()
    } else {
      next()
    }
  }

  if (localStorage.rolltype == "1") {
    if (to.name.indexOf("teacher") > -1) {
      showAlert();
    } else {
      next()
    }
  }
  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
